#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-10-28 9:53 上午
# @Author  : yangdy
# @File    : 2020-10-28.3.回文素数
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def primePalindrome(self, N: int) -> int:

        def is_palindrome(data):
            for i in range(len(data)//2):
                if data[i] != data[len(data)-1-i]:
                    return False
            return True

        def next_palindrome(n):
            ds, xx = [], n
            while xx:
                ds.append(xx % 10)
                xx //= 10
            if n == 10 ** len(ds) - 1:
                return n + 2
            ds.reverse()
            if not is_palindrome(ds):
                u = (len(ds) - 1) // 2

                pass
            u = (len(ds) - 1) // 2
            while u >= 0:
                if ds[u] < 9:
                    ds[u] += 1
                    e = len(ds) - 1 - u
                    if e != u:
                        ds[e] += 1
                    break
                # ds[u], ds[len(ds)-1-u]

            return n

        def is_prime(n):
            xx = 2
            while xx * xx <= n:
                if n % xx == 0:
                    return False
            return True

        x = N
        while x <= 200000000:
            x = next_palindrome(x)
            if is_prime(x):
                return x
        return 0


questions = [
    (6,),
    (8,),
    (13,),
]

work(questions, lambda q: Solution().primePalindrome(q[0]))



